<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/4/15
 * Time: 9:37
 */
namespace app\admin\controller;
use think\Db;
class Order extends Base{

    public function index(){
        if(request()->isAjax()){
            $this->timeOverOrder();
            $params = input('param.');
            $where['a.order_status'] = 1;
            $where['a.order_after'] = ['neq', 3];
            //日期
            if(isset($params['add_start']) && $params['add_start'] != '' && isset($params['add_end']) && $params['add_end'] != ''){
                $where['a.order_time'] = ['between', [$params['add_start'], $params['add_end']]];
            }
            if(isset($params['add_start']) && $params['add_start'] != '' && isset($params['add_end']) && $params['add_end'] == ''){
                $where['a.order_time'] = ['>=', $params['add_start']];
            }if(isset($params['add_start']) && $params['add_start'] == '' && isset($params['add_end']) && $params['add_end'] != ''){
                $where['a.order_time'] = ['<=', $params['add_end']];
            }
            //关键字
            if(isset($params['keyword']) && $params['keyword'] != ''){
                $keyword = $params['keyword'];
                $where['uuu.user_id|uuu.user_username'] = array('like',"%$keyword%");
            }
            if(isset($params['province']) && $params['province'] != ''){
                $where['a.order_sheng'] =$params['province'] ;
            }
            if(isset($params['city']) && $params['city'] != ''){
                $where['a.order_shi'] = $params['city'];
            }
            if(isset($params['district']) && $params['district'] != ''){
                $where['a.order_qu'] = $params['district'];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
             $count =Db::table('up_order')
                    ->alias('a')
                     ->join('up_u_user uuu','uuu.user_id = a.order_userid','left')
                     ->where($where)
                     ->count();
            $count2 =Db::table('up_order')
                ->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.order_userid','left')
                ->where($where)
                ->column('order_userid');
            $lists = Db::table('up_order')
                ->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.order_userid','left')
                ->where($where)
                ->field('a.*,uuu.user_id,uuu.user_username')
                ->order('order_time DESC')
                ->page($Nowpage,$limits)
                ->select();
            foreach ($lists as $key=>&$vo){
                $district =Db::table('up_area')->where('district_id',$vo['order_sheng'])->value('district');
                $district1 =Db::table('up_area')->where('district_id',$vo['order_shi'])->value('district');
                $district2 =Db::table('up_area')->where('district_id',$vo['order_qu'])->value('district');
                $vo['address'] = $district.$district1.$district2.$vo['order_address'];
                if($vo['order_is_coupon']==1){//使用优惠卷
                    $coupon = Db::table('up_my_coupon')->alias('a')
                         ->join('up_coupon uc','uc.coupon_id = a.mc_coupon_id','left')
                         ->field('uc.coupon_name,uc.coupon_price,uc.coupon_start,uc.coupon_end')
                         ->where('a.mc_id',$vo['order_my_coupon'])
                         ->find();
                    $vo['coupon_name'] = $coupon['coupon_name'];
                    $vo['coupon_price'] = $coupon['coupon_price'];
//                    $vo['time'] = $coupon['coupon_start'].' '.$coupon['coupon_end'];
                    $vo['time'] = date('Y-m-d',strtotime($coupon['coupon_start'])).' 至 '.date('Y-m-d',strtotime($coupon['coupon_end']));

                }else{//没有使用优惠卷
                    $vo['coupon_name'] = '';
                    $vo['coupon_price'] = '';
                    $vo['time'] = '';
                }
            }
            $str = '';
            if($count2){
                $str = implode($count2,',');
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'count2' => $str, 'data' => $lists]);
        }
        if(session('?order_id')){
            session('order_id',null);
        }
        $this->timeOverOrder();
        $province = Db::table('up_area')->where('level', 1)->select();
        $this->assign('province', $province);
        return $this->fetch();
    }
    /***
     * @return \think\response\Json
     * 删除
     */
    public function del(){
        $ids = input('ids');
        if($ids == ''){
            return json(['code' => 100, 'msg' => '请选择数据']);
        }
        $where['order_id'] = array('IN',$ids);
        $res = Db::table('up_order')->where('order_id', 'IN', $ids)->setField('order_del', 2);
        if($res){
            return json(['code' => 200, 'msg' => "操作成功"]);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }

    /***
     * 批量导出
     */
    public function excelIn(){
        $ids = input('param.ids');
        if($ids == ''){
            return json(['code' => 100, 'msg' => '请选择数据']);
        }
        $where['order_id'] = array('IN',$ids);
        $lists = Db::table('up_order')
            ->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.order_userid','left')
            ->where($where)
            ->field('a.order_time,a.order_is_coupon,a.order_my_coupon
                  ,uuu.user_id,uuu.user_username
                  ,a.order_sheng,a.order_shi,a.order_qu,a.order_address,a.order_name,a.order_phone')
            ->select();
        foreach ($lists as $key=>&$vo){
            $district =Db::table('up_area')->where('district_id',$vo['order_sheng'])->value('district');
            $district1 =Db::table('up_area')->where('district_id',$vo['order_shi'])->value('district');
            $district2 =Db::table('up_area')->where('district_id',$vo['order_qu'])->value('district');
            $vo['address'] = $district.$district1.$district2.$vo['order_address'];
            if($vo['order_is_coupon']==1){//使用优惠卷
                $coupon = Db::table('up_my_coupon')->alias('a')
                    ->join('up_coupon uc','uc.coupon_id = a.mc_coupon_id','left')
                    ->field('uc.coupon_name,uc.coupon_price,uc.coupon_start,uc.coupon_end
                    ')
                    ->where('a.mc_coupon_id',$vo['order_my_coupon'])
                    ->find();
                $vo['coupon_name'] = $coupon['coupon_name'];
                $vo['coupon_price'] = $coupon['coupon_price'];
                $vo['time'] = $coupon['coupon_start'].' '.$coupon['coupon_end'];
            }else{//没有使用优惠卷
                $vo['coupon_name'] = '';
                $vo['coupon_price'] = '';
                $vo['time'] = '';
            }
            unset($lists[$key]['order_is_coupon']);
            unset($lists[$key]['order_my_coupon']);
            unset($lists[$key]['order_sheng']);
            unset($lists[$key]['order_shi']);
            unset($lists[$key]['order_qu']);
            unset($lists[$key]['order_address']);
        }
        $cellname = [
            ['order_time','下单时间',20,'LEFT'],
            ['user_id','用户ID',15,'LEFT'],
            ['user_username','姓名',15,'LEFT'],
            ['order_name','收货人',15,'LEFT'],
            ['order_phone','收货手机号码',15,'LEFT'],
            ['address','收货地址',15,'LEFT'],
            ['coupon_name','姓名',15,'LEFT'],
            ['coupon_price','优惠卷价格',15,'LEFT'],
            ['time','有效期',15,'LEFT'],
        ];
        $res = exportExcel('待支付订单列表','待支付订单列表',$cellname,$lists);
        return json($res);
    }

    /***
     * @return mixed|\think\response\Json
     * 商品信息
     */
    public function detail(){
        if(request()->isAjax()){
            $id = input('param.id');
            $where['oi_order_id'] = $id;
            $count = Db::table('up_order_info')->where($where)->count();
            $lists = Db::table('up_order_info')->where($where)->select();
            foreach ($lists as $key=>&$vo){
                if($vo['oi_type']==1){
                    $info = Db::table('up_good_spec')->where('spec_id',$vo['oi_spec_id'])->find();
                    $vo['spec'] = $info['spec_name'];
                    $vo['price'] = $vo['oi_price']*$vo['oi_num'].'人民币';
                }else{
                    $vo['spec'] = '';
                    $vo['price'] = $vo['oi_price']*$vo['oi_num'].'金币';
                }
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $id = input('id');
        if($id != ''){
            session('order_id',$id);
        }
        return $this->fetch();
    }

    /***
     * 待支付订单商品导出
     */
    public function excelGood(){
        $ids = input('param.ids');
        if($ids == ''){
            return json(['code' => 100, 'msg' => '请选择数据']);
        }
        $where['oi_id'] = array('IN',$ids);
        $lists = Db::table('up_order_info')->where($where)->select();
        foreach ($lists as $key=>&$vo){
            if($vo['oi_type']==1){
                $info = Db::table('up_good_spec')->where('spec_id',$vo['oi_spec_id'])->find();
                $vo['spec'] = $info['spec_name'];
                $vo['price'] = $vo['oi_price']*$vo['oi_num'].'人民币';
            }else{
                $vo['spec'] = '';
                $vo['price'] = $vo['oi_price']*$vo['oi_num'].'金币';
            }
            unset($lists[$key]['oi_id']);
            unset($lists[$key]['oi_order_id']);
            unset($lists[$key]['oi_good_thumb']);
            unset($lists[$key]['oi_good_coding']);
            unset($lists[$key]['oi_pay_time']);
            unset($lists[$key]['oi_type']);
            unset($lists[$key]['oi_spec_id']);
        }
        $order_number = Db::table('up_order')->where('order_id',session('order_id'))->value('order_number');
        $cellname = [
            ['oi_good_id','商品ID',20,'LEFT'],
            ['oi_price','商品单价',20,'LEFT'],
            ['oi_num','购物数量',15,'LEFT'],
            ['oi_good_name','商品名称',20,'LEFT'],
            ['spec','商品规格',15,'LEFT'],
            ['price','商品总价格',15,'LEFT'],
        ];
        $res = exportExcel('待支付订单'.$order_number,$order_number,$cellname,$lists);
        return json($res);
    }
    
    /**
     * 将半小时前的订单状态改为超时
     * @param $type   适用范围    1 进入订单列表调用   2 进入订单详情调用
     * @param $order_id   进入详情时的订单ID
     */
    public function timeOverOrder(){
            $time = date('Y-m-d H:i:s', strtotime('-30 minute'));
            $where['order_time'] = ['<=', $time];//下单时间为半个小时之前的
            $where['order_del'] = 1;
            $where['order_status'] = 1;//未支付
            $overOrder_ids = Db::table('up_order')
                ->where($where)
                ->column('order_id');
            Db::table('up_order')->where('order_id', 'IN', $overOrder_ids)->setField('order_status', 5);
    }
}